//
//  AuctionCollectionHeaderView.m
//  MyNewProject
//
//  Created by 王春辉 on 2019/12/22.
//  Copyright © 2019年 sun. All rights reserved.
//

#import "AuctionCollectionHeaderView.h"
#import "AuctionCollectionHeaderBtnsView.h"
#import "SDCycleScrollView.h"
#import "SDCollectionViewCell.h"
#import "AuctionRecommendCollectionViewCell.h"


#pragma mark -- mark 数据模型
#import "AuctionHomeModel.h"

#define MARGIN 15.0

@interface AuctionCollectionHeaderView()<SDCycleScrollViewDelegate> {
    
}

@property (nonatomic, copy) AuctionCollectionHeaderViewBannerClickBlock bannerBlock;
@property (nonatomic, copy) AuctionCollectionHeaderViewDetermineDetailClickBlock determineBlock;
@property (nonatomic, copy) AuctionCollectionHeaderViewRecommendMoreButtonClickBlock recommendMoreBlock;
@property (nonatomic, copy) AuctionCollectionHeaderViewRecommendSelectItemAtIndexPathBlock recommendSelectBlock;

/**
 轮播图
 */
@property (nonatomic, strong) SDCycleScrollView *bannerView;

/**
 热门推荐
 */
@property (nonatomic, strong) SDCycleScrollView *recommendView;

@end
@implementation AuctionCollectionHeaderView
@synthesize headerBtnsView = _headerBtnsView;
//@synthesize couponView = _couponView;
@synthesize recommandCellData = _recommandCellData;

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = SKColorWithHex(0xF7F7F7);
        [self setupContentViews];
    }
    return self;
}

- (void)setupContentViews {
    UIImageView *determineImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"auction_header_des_tip_img"]];
    UILabel *determineTitleLabel = [[UILabel alloc] init];
    UILabel *determineDetailLabel = [[UILabel alloc] init];
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [detailButton addTarget:self action:@selector(determineDetailButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:determineImageView];
    [self addSubview:determineTitleLabel];
    [self addSubview:determineDetailLabel];
    [self addSubview:detailButton];
    
    [detailButton setTitle:@"查看详情" forState:UIControlStateNormal];
    [detailButton setTitleColor:SKColorWithHex(0xC6AF83) forState:UIControlStateNormal];
    [detailButton setImage:SKIMAGE_NAMED(@"auction_header_show_recommend_detail_btn_img") forState:UIControlStateNormal];
    detailButton.titleLabel.font = SKFONT_Medium(11);
    CGFloat labelWidth = detailButton.titleLabel.intrinsicContentSize.width;
    CGFloat imageWidth = detailButton.imageView.frame.size.width;
    detailButton.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth - 10, 0, imageWidth + 10);
    detailButton.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0,  -labelWidth);
    
    determineTitleLabel.text = @"鉴真宝·国家认定检测机构合作";
    determineDetailLabel.text = @"假一赔百万 国家权威鉴定 暂仅支持珠宝玉石部分商品";
    
    determineTitleLabel.textColor = SKColorWithHex(0xC6AF83);
    determineDetailLabel.textColor = SKColorWithHex(0xC6AF83);
    
    determineTitleLabel.font = SKFONT_Semibold(13);
    determineDetailLabel.font = SKFONT_Medium(10);
    
    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(MARGIN);
        make.right.mas_offset(-MARGIN);
        make.height.mas_equalTo(self.bannerView.mas_width).multipliedBy(160.0 / 345.0);
    }];
    
    [determineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(MARGIN);
        make.top.mas_equalTo(self.bannerView.mas_bottom).mas_offset(MARGIN);
        make.size.mas_equalTo(CGSizeMake(25, 30));
    }];
    
    [detailButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-MARGIN);
        make.centerY.mas_equalTo(determineImageView);
    }];
    
    [determineTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(determineImageView.mas_right).mas_offset(7.5);
        make.bottom.mas_equalTo(determineImageView.mas_centerY);
    }];
    
    [determineDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(determineTitleLabel);
        make.top.mas_equalTo(determineImageView.mas_centerY);
    }];
    
    UIView *line_1 = [[UIView alloc] init];
    line_1.backgroundColor = SKColorWithHex(0xE5E5E5);
    [self addSubview:line_1];
    
    [line_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(MARGIN);
        make.right.mas_offset(-MARGIN);
        make.top.mas_equalTo(determineImageView.mas_bottom).mas_offset(MARGIN);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.headerBtnsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.bannerView);
        make.top.mas_equalTo(line_1.mas_bottom).mas_offset(MARGIN);
        make.height.mas_equalTo(self.headerBtnsView.mas_width).multipliedBy(70.0/345);
    }];
    
//    [self.couponView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(self.bannerView);
//        make.height.mas_equalTo(self.couponView.mas_width).multipliedBy(187.0/345);
//        make.top.mas_equalTo(self.headerBtnsView.mas_bottom).mas_offset(21);
//    }];
    
    // TODO: 添加一个背景图片
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"auction_header_show_recommend_bg_img")];
    [self addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerBtnsView.mas_bottom).mas_offset(21);
        make.left.right.mas_offset(0);
    }];
    
    
    UIImageView *shopRecommendImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"auction_section_tip_shop_recomment_img"]];
    [self addSubview:shopRecommendImageView];
    
    UILabel *shopRecommendTitleLabel = [[UILabel alloc] init];
    shopRecommendTitleLabel.text = @"商家推荐";
    shopRecommendTitleLabel.textColor = SKColorWithHex(0x333333);
    shopRecommendTitleLabel.font = SKFONT_Semibold(18);
    [self addSubview:shopRecommendTitleLabel];

    UILabel *shopRecommendDetailLabel = [[UILabel alloc] init];
    shopRecommendDetailLabel.text = @"带你原产地寻宝捡漏";
    shopRecommendDetailLabel.textColor = SKColorWithHex(0x888888);
    shopRecommendDetailLabel.font = SKFONT_Medium(13);
    [self addSubview:shopRecommendDetailLabel];
    
    UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreButton setTitle:@"查看更多" forState:UIControlStateNormal];
    [moreButton setImage:SKIMAGE_NAMED(@"auction_header_show_detail_btn_img") forState:UIControlStateNormal];
    moreButton.titleLabel.font = SKFONT_Medium(11);
    [moreButton setTitleColor:SKColorWithHex(0xFF4C30) forState:UIControlStateNormal];
    moreButton.backgroundColor = [UIColor whiteColor];
    [moreButton addTarget:self action:@selector(recommendMoreButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    moreButton.tag = 521;
    [self addSubview:moreButton];
    
    CGFloat more_btn_w = 78.0;
    CGFloat more_btn_h = more_btn_w / 78.0 * 24;
    moreButton.layer.masksToBounds = true;
    moreButton.layer.cornerRadius = more_btn_h * 0.5;
    
    CGFloat moreLabelWidth = moreButton.titleLabel.intrinsicContentSize.width;
    CGFloat moreImageWidth = moreButton.imageView.frame.size.width;
    moreButton.titleEdgeInsets = UIEdgeInsetsMake(0, -moreImageWidth - 10, 0, moreImageWidth);
    moreButton.imageEdgeInsets = UIEdgeInsetsMake(0, moreLabelWidth, 0,  -moreLabelWidth - 5);

    [shopRecommendImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(MARGIN);
        make.top.mas_equalTo(self.headerBtnsView.mas_bottom).mas_offset(36);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    [shopRecommendTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(shopRecommendImageView.mas_right).mas_offset(10);
        make.centerY.mas_equalTo(shopRecommendImageView);
    }];
    
    [shopRecommendDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(shopRecommendTitleLabel.mas_right).mas_offset(10);
        make.centerY.mas_equalTo(shopRecommendTitleLabel);
        make.right.mas_lessThanOrEqualTo(moreButton.mas_left).mas_offset(-10);
    }];
    
    [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-MARGIN);
        make.centerY.mas_equalTo(shopRecommendImageView);
        make.size.mas_equalTo(CGSizeMake(more_btn_w, more_btn_h));
    }];
    
    [self.recommendView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.bannerView);
        make.top.mas_equalTo(shopRecommendImageView.mas_bottom).mas_offset(MARGIN);
        make.height.mas_equalTo(self.recommendView.mas_width).multipliedBy(200.0/345);
    }];
    
    UIView *line_2 = [[UIView alloc] init];
    line_2.backgroundColor = SKColorWithHex(0xE5E5E5);
    [self addSubview:line_2];
    [line_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(line_1);
        make.top.mas_equalTo(self.recommendView.mas_bottom).mas_offset(MARGIN);
        make.height.mas_equalTo(0.5);
    }];
    
    UIImageView *hotRecommendImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"auction_section_tip_hot_recomment_img"]];
    [self addSubview:hotRecommendImageView];
    
    UILabel *hotRecommendTitleLabel = [[UILabel alloc] init];
    hotRecommendTitleLabel.text = @"热门推荐";
    hotRecommendTitleLabel.font = shopRecommendTitleLabel.font;
    hotRecommendTitleLabel.textColor = shopRecommendTitleLabel.textColor;
    [self addSubview:hotRecommendTitleLabel];
    
    UILabel *hotRecommendDetailLabel = [[UILabel alloc] init];
    hotRecommendDetailLabel.text = @"为您精选热门好物";
    hotRecommendDetailLabel.textColor = shopRecommendDetailLabel.textColor;
    hotRecommendDetailLabel.font = shopRecommendDetailLabel.font;
    [self addSubview:hotRecommendDetailLabel];
    
    [hotRecommendImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(MARGIN);
        make.top.mas_equalTo(line_2.mas_bottom).mas_offset(25);
        make.size.mas_equalTo(shopRecommendImageView);
    }];
    
    [hotRecommendTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(shopRecommendTitleLabel);
        make.centerY.mas_equalTo(hotRecommendImageView);
    }];
    
    [hotRecommendDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(hotRecommendTitleLabel.mas_right).mas_offset(10);
        make.centerY.mas_equalTo(hotRecommendTitleLabel);
        make.right.mas_lessThanOrEqualTo(self).mas_offset(-MARGIN);
        make.bottom.mas_offset(-10);
    }];
}

#pragma mark -- mark 懒加载

- (SDCycleScrollView *)bannerView {
    if (!_bannerView) {
        _bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero shouldInfiniteLoop:true imageNamesGroup:@[]];
        _bannerView.placeholderImage = SKIMAGE_NAMED(@"bannerPlacehoder");
        _bannerView.currentPageDotImage = SKIMAGE_NAMED(@"shop_banner_pagecontrol_current_img");
        _bannerView.pageDotImage = SKIMAGE_NAMED(@"shop_banner_pagecontrol_default_img");
        _bannerView.layer.masksToBounds = true;
        _bannerView.layer.cornerRadius = 6.0f;
        [self addSubview:_bannerView];
    }
    return _bannerView;
}

- (AuctionCollectionHeaderBtnsView *)headerBtnsView {
    if (!_headerBtnsView) {
        _headerBtnsView = [[AuctionCollectionHeaderBtnsView alloc] init];
        [self addSubview:_headerBtnsView];
    }
    return _headerBtnsView;
}

//- (AuctionNewUserEnjoyView *)couponView {
//    if (!_couponView) {
//        _couponView = [[AuctionNewUserEnjoyView alloc] init];
//        _couponView.backgroundColor = [UIColor lightGrayColor];
//        [self addSubview:_couponView];
//    }
//    return _couponView;
//}
- (SDCycleScrollView *)recommendView {
    if (!_recommendView) {
        _recommendView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero shouldInfiniteLoop:true imageNamesGroup:@[]];
        _recommendView.placeholderImage = SKIMAGE_NAMED(@"bannerPlacehoder");
        _recommendView.currentPageDotImage = SKIMAGE_NAMED(@"shop_recomend_pagecontrol_current_img");
        _recommendView.pageDotImage = SKIMAGE_NAMED(@"shop_recomend_pagecontrol_default_img");
        _recommendView.delegate = self;
        [self addSubview:_recommendView];
    }
    return _recommendView;
}
- (NSArray<NSArray *> *)recommandCellData {
    if (!_recommandCellData) {
        _recommandCellData = [NSArray array];
    }
    return _recommandCellData;
}
#pragma mark -- mark 业务逻辑
- (void)setBannerImageNames:(NSArray<NSString *> *)bannerImageNames {
    _bannerImageNames = bannerImageNames;
    self.bannerView.imageURLStringsGroup = bannerImageNames;
}

- (void)setRecommendDatas:(NSArray<AuctionHomeShopModel *> *)recommendDatas {
    _recommendDatas = recommendDatas;
    NSInteger perCellCounts = 3;
    NSInteger count = (recommendDatas.count / perCellCounts) + ((recommendDatas.count % perCellCounts) > 0 ? 1 : 0);
    NSMutableArray *_arr = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i < count; i++) {
        [_arr addObject:@""];
    }
    self.recommendView.localizationImageNamesGroup = _arr;
    NSMutableArray <NSArray <AuctionHomeShopModel *>*>*mArr = [NSMutableArray array];
    NSMutableArray *recomentsMarr = [NSMutableArray arrayWithArray:recommendDatas];
    for (int i = 0; i < recomentsMarr.count; i+=perCellCounts) {
        NSRange range = NSMakeRange(i, perCellCounts);
        NSArray *temArr = [recomentsMarr subarrayWithRange:range];
        [mArr addObject:temArr];
        [recomentsMarr removeObjectsInRange:range];
    }
    [mArr addObject:recomentsMarr];
    _recommandCellData = mArr;
}
#pragma mark -- mark EVENT

/**
 鉴珍宝查看详情

 @param sender sender
 */
- (void)determineDetailButtonClick:(UIButton *)sender {
    if (self.determineBlock) {
        self.determineBlock();
    }
}

/**
 推荐更多

 @param sender sender
 */
- (void)recommendMoreButtonClick:(UIButton *)sender {
    if (self.recommendMoreBlock) {
        self.recommendMoreBlock();
    }
}

/**
 点击使用优惠券

 @param sender sender
 */
- (void)couponsButtonClick:(UIButton *)sender {
    
}

/**
 轮播banner点击事件回调
 
 @param block block
 */
- (void)bannerClickBlock:(AuctionCollectionHeaderViewBannerClickBlock)block {
    self.bannerBlock = block;
}

/**
 鉴宝详情按钮点击事件回调
 
 @param block block
 */
- (void)determineDetailClickBlock:(AuctionCollectionHeaderViewDetermineDetailClickBlock)block {
    self.determineBlock = block;
}

/**
 那四个按钮点击事件回调
 
 @param block block
 */
- (void)headerButtonsClickBlock:(AuctionCollectionHeaderViewBtnsClickBlock)block {
    [self.headerBtnsView buttonsClickBlock:block];
}

/**
 优惠券点击事件回调
 
 @param block block
 */
- (void)couponsButtonClickBlock:(AuctionCollectionHeaderViewCouponButtonClickBlock)block {
//    [self.couponView setCouponUseButtonClickBlock:block];
}

/// 优惠券点击商品
/// @param block block
- (void)couponsProductButtonClickBlock:(void(^)(NSInteger))block {
//    self.couponView.couponProductButtonClickBlock = block;
}

/**
 商家推荐更多按钮点击回调
 
 @param block block
 */
- (void)recommendMoreButtonClickBlock:(AuctionCollectionHeaderViewRecommendMoreButtonClickBlock)block {
    self.recommendMoreBlock = block;
}

/**
 商家推荐cell点击事件回调
 
 @param block block
 */
- (void)recommendSelectAtItemIndexPathBlock:(AuctionCollectionHeaderViewRecommendSelectItemAtIndexPathBlock)block {
    self.recommendSelectBlock = block;
}

#pragma mark -- mark SDCycleScrollViewDelegate
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    if (cycleScrollView == self.bannerView) {
        if (self.bannerBlock) {
            self.bannerBlock(index); }
    }
}

/** 如果你需要自定义cell样式，请在实现此代理方法返回你的自定义cell的class。 */
- (Class)customCollectionViewCellClassForCycleScrollView:(SDCycleScrollView *)view {
    return [AuctionRecommendCollectionViewCell class];
}

/** 如果你自定义了cell样式，请在实现此代理方法为你的cell填充数据以及其它一系列设置 */
- (void)setupCustomCell:(UICollectionViewCell *)cell forIndex:(NSInteger)index cycleScrollView:(SDCycleScrollView *)view {
    self.recommendView.placeholderImage = [UIImage new];
    AuctionRecommendCollectionViewCell *recommendCell = (AuctionRecommendCollectionViewCell *)cell;
    SKWeakSelf;
    recommendCell.cellTapGestureBlock = ^(NSInteger row) {
        if (weakSelf.recommendSelectBlock) {
            weakSelf.recommendSelectBlock([NSIndexPath indexPathForRow:row inSection:index]);
        }
    };
    NSArray <AuctionHomeShopModel *>*data = self.recommandCellData[index];
    recommendCell.datas = data;
}
@end
